package com.handler.base;

import com.event.ProcessorEvent;
import com.handler.baseinterface.IPrintExecHandler;
import com.handler.game.BaseHandler;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author : gameZhao
 * @version V1.0
 * @Project: disruptorDemo
 * @Package com.handler
 * @Description: handler模板
 * @date Date : 2021年12月27日 18:09
 */
public abstract class BaseProcessorHandler implements IPrintExecHandler {
    
    protected static AtomicLong execNum = new AtomicLong();
    
    protected static AtomicLong execAloneNum = new AtomicLong();
    
    protected String processorName;
    
    
    protected long DEFAULT_LOG_WAIT_TIME = 2000L;
    
    protected int DEFAULT_PRINT_SIZE = 200;
    
    protected int DEFAULT_LOG_DO_TIME = 200;
    
    protected void execHandler(ProcessorEvent processorEvent) {
        
        try {
            var handler = processorEvent.getHandler();
            var begin = System.currentTimeMillis();
            var waitTime = begin - handler.getTime();
            handler.action();
            long doTime = System.currentTimeMillis() - begin;
            doPrint(handler, doTime);
            waitPrint(handler, waitTime);
            //sizePrint()
            execNum.incrementAndGet();
            execAloneNum.incrementAndGet();
        } catch (Exception e) {
            // TODO LOG
        } finally {
            processorEvent.release();
        }
    }
    
    
    @Override
    public void waitPrint(BaseHandler handler, long waitTime) {
        if (waitTime > DEFAULT_LOG_WAIT_TIME) {
            //TODO PRINT LOG
        }
    }
    
    @Override
    public void doPrint(BaseHandler handler, long doTime) {
        
        if (doTime > DEFAULT_LOG_DO_TIME) {
            //TODO PRINT LOG
        }
    }
    
    @Override
    public void sizePrint(BaseHandler handler, int size) {
    
    }
}
